﻿Public Class FrmChech


    Public Sub InitBaoYang()
        Try

            Dim sql As String = "select * from Maintainace"
            Dim ds As DataSet = New DataSet
            ds = SearchMysql(sql)
            For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                If ds.Tables(0).Rows(i)("MaintanceTime").ToString <> "" Then
                    Dim time As DateTime = ds.Tables(0).Rows(i)("MaintanceTime").ToString
                    Dim TimeAfterSeven As DateTime
                    If ds.Tables(0).Rows(i)("MaintanceType").ToString = "1" Then
                        TimeAfterSeven = time.AddDays(MaintainInfo.Feeder_MaintanceCycle)
                    ElseIf ds.Tables(0).Rows(i)("MaintanceType").ToString = "2" Then
                        TimeAfterSeven = time.AddDays(MaintainInfo.Screwdriver_MaintanceCycle)
                    ElseIf ds.Tables(0).Rows(i)("MaintanceType").ToString = "3" Then
                        TimeAfterSeven = time.AddDays(MaintainInfo.Motor_MaintanceCycle)
                    ElseIf ds.Tables(0).Rows(i)("MaintanceType").ToString = "4" Then
                        TimeAfterSeven = time.AddDays(MaintainInfo.Nozzle_MaintanceCycle)
                    ElseIf ds.Tables(0).Rows(i)("MaintanceType").ToString = "5" Then
                        TimeAfterSeven = time.AddDays(MaintainInfo.Screw_MaintanceCycle)
                    ElseIf ds.Tables(0).Rows(i)("MaintanceType").ToString = "6" Then
                        TimeAfterSeven = time.AddDays(MaintainInfo.Cable_MaintanceCycle)
                    ElseIf ds.Tables(0).Rows(i)("MaintanceType").ToString = "7" Then
                        TimeAfterSeven = time.AddDays(MaintainInfo.Blet_MaintanceCycle)
                    ElseIf ds.Tables(0).Rows(i)("MaintanceType").ToString = "8" Then
                        TimeAfterSeven = time.AddDays(MaintainInfo.Clean_MaintanceCycle)
                    ElseIf ds.Tables(0).Rows(i)("MaintanceType").ToString = "9" Then
                        TimeAfterSeven = time.AddDays(MaintainInfo.Robot_MaintanceCycle)
                    End If

                    Dim CurrentTime As DateTime = DateTime.Now
                    Dim LevelHour As Double = Math.Round((TimeAfterSeven.Ticks - CurrentTime.Ticks) / 10000 / 1000 / 60 / 60, 1)
                    If ds.Tables(0).Rows(i)("MaintanceType").ToString = "1" Then
                        If LevelHour <= 0 Then
                            Label12.Text = "保养时间已到"
                            Label12.ForeColor = Color.Red
                            FeederChageProgressBar.Value = 100
                        Else
                            Label12.Text = "剩余" & LevelHour & "小时"
                            Label12.ForeColor = Color.Black
                            FeederChageProgressBar.Value = ((MaintainInfo.Feeder_MaintanceCycle * 24) - LevelHour) / (MaintainInfo.Feeder_MaintanceCycle * 24) * 100
                        End If

                    ElseIf ds.Tables(0).Rows(i)("MaintanceType").ToString = "2" Then
                        If LevelHour <= 0 Then
                            Label14.Text = "保养时间已到"
                            Label14.ForeColor = Color.Red
                            ScrewHeadProgressBar.Value = 100
                        Else
                            Label14.Text = "剩余" & LevelHour & "小时"
                            Label14.ForeColor = Color.Black
                            ScrewHeadProgressBar.Value = ((MaintainInfo.Screwdriver_MaintanceCycle * 24) - LevelHour) / (MaintainInfo.Screwdriver_MaintanceCycle * 24) * 100
                        End If

                    ElseIf ds.Tables(0).Rows(i)("MaintanceType").ToString = "3" Then
                        If LevelHour <= 0 Then
                            Label34.Text = "保养时间已到"
                            Label34.ForeColor = Color.Red
                            RelayChangeProgressBar.Value = 100
                        Else
                            Label34.Text = "剩余" & LevelHour & "小时"
                            Label34.ForeColor = Color.Black
                            RelayChangeProgressBar.Value = ((MaintainInfo.Motor_MaintanceCycle * 24) - LevelHour) / (MaintainInfo.Motor_MaintanceCycle * 24) * 100
                        End If

                    ElseIf ds.Tables(0).Rows(i)("MaintanceType").ToString = "4" Then
                        If LevelHour <= 0 Then
                            Label38.Text = "保养时间已到"
                            Label38.ForeColor = Color.Red
                            NozzleChangeProgressBar.Value = 100
                        Else
                            Label38.Text = "剩余" & LevelHour & "小时"
                            Label38.ForeColor = Color.Black
                            NozzleChangeProgressBar.Value = ((MaintainInfo.Nozzle_MaintanceCycle * 24) - LevelHour) / (MaintainInfo.Nozzle_MaintanceCycle * 24) * 100
                        End If

                    ElseIf ds.Tables(0).Rows(i)("MaintanceType").ToString = "5" Then
                        If LevelHour <= 0 Then
                            Label2.Text = "保养时间已到"
                            Label2.ForeColor = Color.Red
                            ScrewProgressBar.Value = 100
                            MaintainInfo.Screw_Status = 0
                        Else
                            Label2.Text = "剩余" & LevelHour & "小时"
                            Label2.ForeColor = Color.Black
                            ScrewProgressBar.Value = ((MaintainInfo.Screw_MaintanceCycle * 24) - LevelHour) / (MaintainInfo.Screw_MaintanceCycle * 24) * 100
                        End If

                    ElseIf ds.Tables(0).Rows(i)("MaintanceType").ToString = "6" Then
                        If LevelHour <= 0 Then
                            Label4.Text = "保养时间已到"
                            Label4.ForeColor = Color.Red
                            HeadCableProgressBar.Value = 100
                            MaintainInfo.Cable_Status = 0
                        Else
                            Label4.Text = "剩余" & LevelHour & "小时"
                            Label4.ForeColor = Color.Black
                            HeadCableProgressBar.Value = ((MaintainInfo.Cable_MaintanceCycle * 24) - LevelHour) / (MaintainInfo.Cable_MaintanceCycle * 24) * 100
                        End If

                    ElseIf ds.Tables(0).Rows(i)("MaintanceType").ToString = "7" Then
                        If LevelHour <= 0 Then
                            Label6.Text = "保养时间已到"
                            Label6.ForeColor = Color.Red
                            BletProgressBar.Value = 100
                            MaintainInfo.Blet_Status = 0
                        Else
                            Label6.Text = "剩余" & LevelHour & "小时"
                            Label6.ForeColor = Color.Black
                            CleanVacuumProgressBar.Value = ((MaintainInfo.Blet_MaintanceCycle * 24) - LevelHour) / (MaintainInfo.Blet_MaintanceCycle * 24) * 100
                        End If
                    ElseIf ds.Tables(0).Rows(i)("MaintanceType").ToString = "8" Then
                        If LevelHour <= 0 Then
                            Label8.Text = "保养时间已到"
                            Label8.ForeColor = Color.Red
                            FeederChageProgressBar.Value = 100

                        Else
                            Label8.Text = "剩余" & LevelHour & "小时"
                            Label8.ForeColor = Color.Black
                            CleanVacuumProgressBar.Value = ((MaintainInfo.Clean_MaintanceCycle * 24) - LevelHour) / (MaintainInfo.Clean_MaintanceCycle * 24) * 100
                        End If
                    ElseIf ds.Tables(0).Rows(i)("MaintanceType").ToString = "9" Then
                        If LevelHour <= 0 Then
                            Label10.Text = "保养时间已到"
                            Label10.ForeColor = Color.Red
                            RobotProgressBar.Value = 100
                            MaintainInfo.Robot_Status = 0
                        Else
                            Label10.Text = "剩余" & LevelHour & "小时"
                            Label10.ForeColor = Color.Black
                            RobotProgressBar.Value = ((MaintainInfo.Robot_MaintanceCycle * 24) - LevelHour) / (MaintainInfo.Robot_MaintanceCycle * 24) * 100
                        End If
                    End If
                Else
                    If ds.Tables(0).Rows(i)("MaintanceType").ToString = "1" Then
                        Label12.Text = "该项尚未保养过"
                        Label12.ForeColor = Color.Red
                        FeederChageProgressBar.Value = 0
                    ElseIf ds.Tables(0).Rows(i)("MaintanceType").ToString = "2" Then
                        Label14.Text = "该项尚未保养过"
                        Label14.ForeColor = Color.Red
                        ScrewHeadProgressBar.Value = 0
                    ElseIf ds.Tables(0).Rows(i)("MaintanceType").ToString = "3" Then
                        Label34.Text = "该项尚未保养过"
                        Label34.ForeColor = Color.Red
                        RelayChangeProgressBar.Value = 0

                    ElseIf ds.Tables(0).Rows(i)("MaintanceType").ToString = "4" Then
                        Label38.Text = "该项尚未保养过"
                        Label38.ForeColor = Color.Red
                        NozzleChangeProgressBar.Value = 0

                    ElseIf ds.Tables(0).Rows(i)("MaintanceType").ToString = "5" Then
                        Label2.Text = "该项尚未保养过"
                        Label2.ForeColor = Color.Red
                        ScrewProgressBar.Value = 0

                    ElseIf ds.Tables(0).Rows(i)("MaintanceType").ToString = "6" Then
                        Label4.Text = "该项尚未保养过"
                        Label4.ForeColor = Color.Red
                        HeadCableProgressBar.Value = 0

                    ElseIf ds.Tables(0).Rows(i)("MaintanceType").ToString = "7" Then
                        Label6.Text = "该项尚未保养过"
                        Label6.ForeColor = Color.Red
                        BletProgressBar.Value = 0
                    ElseIf ds.Tables(0).Rows(i)("MaintanceType").ToString = "8" Then
                        Label8.Text = "该项尚未保养过"
                        Label8.ForeColor = Color.Red
                        CleanVacuumProgressBar.Value = 0
                    ElseIf ds.Tables(0).Rows(i)("MaintanceType").ToString = "9" Then
                        Label10.Text = "该项尚未保养过"
                        Label10.ForeColor = Color.Red
                        RobotProgressBar.Value = 0
                    End If
                End If
            Next

        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString & ex.StackTrace.ToString, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub BaoYangTimer_Tick(sender As Object, e As EventArgs) Handles BaoYangTimer.Tick
        InitBaoYang()
    End Sub

    Private Sub FrmCheck_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        FrmMain.InitMaintanceInfo()
        BaoYangTimer.Enabled = False
        InitBaoYang()
        ScrewDays.Text = MaintainInfo.Screw_MaintanceCycle
        HeadCableDays.Text = MaintainInfo.Cable_MaintanceCycle
        BletDays.Text = MaintainInfo.Blet_MaintanceCycle
        CleanVacuumDays.Text = MaintainInfo.Clean_MaintanceCycle
        CleanFeederDays.Text = MaintainInfo.Robot_MaintanceCycle
        FeederChangeDays.Text = MaintainInfo.Feeder_MaintanceCycle
        ScrewHeadChangeDays.Text = MaintainInfo.Screwdriver_MaintanceCycle
        RelayChangeDays.Text = MaintainInfo.Motor_MaintanceCycle
        NozzleChangeDays.Text = MaintainInfo.Nozzle_MaintanceCycle
        BaoYangTimer.Enabled = True
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        MaintainInfo.Cable_MaintanceCycle = HeadCableDays.Text
        SaveConfigValue(6, MaintainInfo.Cable_MaintanceCycle)
        MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        MaintainInfo.Blet_MaintanceCycle = BletDays.Text
        SaveConfigValue(7, MaintainInfo.Blet_MaintanceCycle)
        MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        MaintainInfo.Clean_MaintanceCycle = CleanVacuumDays.Text
        SaveConfigValue(8, MaintainInfo.Clean_MaintanceCycle)
        MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        'CleanScrewFeederDays-修改为保养机器人
        MaintainInfo.Robot_MaintanceCycle = CleanFeederDays.Text
        SaveConfigValue(9, MaintainInfo.Robot_MaintanceCycle)
        MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        MaintainInfo.Feeder_MaintanceCycle = FeederChangeDays.Text
        SaveConfigValue(1, MaintainInfo.Feeder_MaintanceCycle)
        MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
    End Sub

    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
        MaintainInfo.Screwdriver_MaintanceCycle = ScrewHeadChangeDays.Text
        SaveConfigValue(2, MaintainInfo.Screwdriver_MaintanceCycle)
        MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        MaintainInfo.Screw_MaintanceCycle = ScrewDays.Text
        SaveConfigValue(5, MaintainInfo.Screw_MaintanceCycle)
        MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
    End Sub
    Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
        MaintainInfo.Motor_MaintanceCycle = RelayChangeDays.Text
        SaveConfigValue(3, MaintainInfo.Motor_MaintanceCycle)
        MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
    End Sub
    Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
        MaintainInfo.Nozzle_MaintanceCycle = NozzleChangeDays.Text
        SaveConfigValue(4, MaintainInfo.Nozzle_MaintanceCycle)
        MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
    End Sub

    ''' <summary>
    ''' 通过维护类型获取上次维护时间
    ''' </summary>
    ''' <param name="MaintanceType"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetMaintanceTimeByMaintanceType(ByVal MaintanceType As Integer) As String
        Dim MaintanceTime As String = ""
        Try
            Dim sql As String = "select * from Maintainace" & " where MaintanceType=" & MaintanceType
            Dim ds As DataSet = New DataSet
            ds = SearchMysql(sql)
            If ds.Tables(0).Rows.Count > 0 Then
                MaintanceTime = ds.Tables(0).Rows(0)("MaintanceTime").ToString
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString & ex.StackTrace.ToString, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        Return MaintanceTime
    End Function

    ''' <summary>
    ''' 保存配置信息到数据库
    ''' </summary>
    ''' <param name="key"></param>
    ''' <param name="value"></param>
    ''' <param name="MachineNo"></param>
    ''' <remarks></remarks>
    Public Sub SaveConfigValue(ByVal type As String, ByVal value As String)
        Try
            Dim excuteSQL As String
            excuteSQL = "update Maintainace" & " set MaintanceCycle=" & "'" & value & "'" & " where MaintanceType=" & "'" & type & "'"

            HandleMysql(excuteSQL)

        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString() & ex.StackTrace.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub
    Public Sub SaveOtherValue(ByVal type As String, ByVal value As String)
        Try
            Dim excuteSQL As String
            excuteSQL = "update Maintainace" & " set Others=" & "'" & value & "'" & " where MaintanceType=" & "'" & type & "'"

            HandleMysql(excuteSQL)

        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString() & ex.StackTrace.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub
    Public Sub SaveStatus(ByVal type As String, ByVal value As String)
        Try
            Dim excuteSQL As String
            excuteSQL = "update Maintainace" & " set Status=" & "'" & value & "'" & " where MaintanceType=" & "'" & type & "'"

            HandleMysql(excuteSQL)

        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString() & ex.StackTrace.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub



    Private Sub Label38_MouseHover(sender As Object, e As EventArgs) Handles Label8.MouseHover, Label6.MouseHover, Label4.MouseHover, Label38.MouseHover, Label34.MouseHover, Label2.MouseHover, Label14.MouseHover, Label12.MouseHover, Label10.MouseHover
        Dim tip As ToolTip = New ToolTip
        Dim lab As Label = sender
        Dim MaintanceTime As String = ""
        If lab.Name = "Label12" Then
            MaintanceTime = GetMaintanceTimeByMaintanceType(1)
        ElseIf lab.Name = "Label14" Then
            MaintanceTime = GetMaintanceTimeByMaintanceType(2)
        ElseIf lab.Name = "Label32" Then
            MaintanceTime = GetMaintanceTimeByMaintanceType(3)
        ElseIf lab.Name = "Label38" Then
            MaintanceTime = GetMaintanceTimeByMaintanceType(4)
        ElseIf lab.Name = "Label2" Then
            MaintanceTime = GetMaintanceTimeByMaintanceType(5)
        ElseIf lab.Name = "Label4" Then
            MaintanceTime = GetMaintanceTimeByMaintanceType(6)
        ElseIf lab.Name = "Label6" Then
            MaintanceTime = GetMaintanceTimeByMaintanceType(7)
        ElseIf lab.Name = "Label8" Then
            MaintanceTime = GetMaintanceTimeByMaintanceType(8)
        ElseIf lab.Name = "Label10" Then
            MaintanceTime = GetMaintanceTimeByMaintanceType(9)
        End If
        If MaintanceTime = "" Then
            tip.SetToolTip(lab, "没有维护记录")
        Else
            tip.SetToolTip(lab, "上次维护时间：" & MaintanceTime)
        End If
    End Sub

    Private Sub NozzleChangeBtn_Click(sender As Object, e As EventArgs) Handles ScrewHeadChangeBtn.Click, ScrewBtn.Click, RelayChangeBtn.Click, NozzleChangeBtn.Click, HeadCableBtn.Click, FeederChangeBtn.Click, CleanVacuumBtn.Click, CleanFeederBtn.Click, BletBtn.Click
        Dim btn As Button = sender
        If btn.Name = "FeederChangeBtn" Then
            MaintanceSearchFlag = 1
        ElseIf btn.Name = "ScrewHeadChangeBtn" Then
            MaintanceSearchFlag = 2
        ElseIf btn.Name = "RelayChangeBtn" Then
            MaintanceSearchFlag = 3
        ElseIf btn.Name = "NozzleChangeBtn" Then
            MaintanceSearchFlag = 4
        ElseIf btn.Name = "ScrewBtn" Then
            MaintanceSearchFlag = 5
        ElseIf btn.Name = "HeadCableBtn" Then
            MaintanceSearchFlag = 6
        ElseIf btn.Name = "BletBtn" Then
            MaintanceSearchFlag = 7
        ElseIf btn.Name = "CleanVacuumBtn" Then
            MaintanceSearchFlag = 8
        ElseIf btn.Name = "CleanFeederBtn" Then
            MaintanceSearchFlag = 9
        End If
        FrmHistoryMaintance.ShowDialog()
    End Sub


End Class